<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Service extends CI_Controller {
    function Service() {
        parent::__construct();
        //$this->load->scaffolding('entries');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
    }
	
    public function index()
    {
        $data['message'] = "";
        
        $this->db->group_by('name');        
        
        $data['make'] = $this->db->get('psacar_make');

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("name", "asc"); 
        $data['service'] = $this->db->get('psacar_service');

        $this->db->order_by("order", "asc"); 
        $this->db->order_by("province", "asc"); 
        $data['location'] = $this->db->get('psacar_location');
        
        $data['template_page'] = 'templates/service/service_center';

        $this->load->view('header',$data);
        $this->load->view('service',$data);
        $this->load->view('footer',$data);
    }
    
    public function result() {        
        $data['message'] = "";

        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;
        
        $service = $this->input->post('service');
        $page_template = '';
        if($service != 'Insurance') {
            $make = $this->input->post('make');
            $model = $this->input->post('model');
            $location = $this->input->post('location');
            $year = $this->input->post('year');

        $data['criteria_list'] = 
            $this->register_search_criteria(                    
                $make, 
                $model, 
                $service, 
                $location, 
                $year);
            
            $this->load->model('Service_model');
            $service_list = $this->Service_model->get_service_list(
                $make, 
                $model, 
                $service,
                $location,
                $year);   
            
            //Pagination        
            $data['pagination'] = $this->generate_pagination(
                'index.php/service/result',
                $service_list->num_rows,
                $limit);

            $service_list = array_slice($service_list->result(), $offset, $limit);
            $data['member_list'] = $service_list;
            
            $page_template = 'templates/member/member_search_result';            
        }
        else {
            $insurance = $this->input->post('insurance');
            $this->load->model('Page_model');
            $data['page_list'] = $this->Page_model->get_page_by_name($insurance);
            
            $page_template = 'templates/page/page_search_result';
        }

        $this->load->view('header', $data);
        $this->load->view($page_template, $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);        
    }
    
    public function detail()
    {
        $data['message'] = "";

        $service = $this->uri->segment(3);        
        if($service)
        {
            $service = str_replace('%20', ' ', $service);
            $data['service_list'] = $this->db->get_where('psacar_service', array('name' => $service));

            /*
            $this->load->model('Dealer_model');
            $data['dealer_list'] = $this->Dealer_model->get_dealer_by_id($dealer_id);
            
            $this->load->model('Service_model');
            $data['service_list'] = '';
            foreach($data['dealer_list']->result() as $row)
            {                 
                $service_id_list = preg_split('/,/', $row->service_id, -1, PREG_SPLIT_OFFSET_CAPTURE);
                foreach($service_id_list as $a_service)
                {
                    if(strlen($a_service[0]) != 0)
                    {
                        $service_list = $this->Service_model->get_service_by_id($a_service[0]);
                        foreach($service_list->result() as $service_row)
                            $data['service_list'] = $data['service_list'] . $service_row->name . ',';
                    }
                }
            }
            
            if(substr($data['service_list'], strlen(',') * -1) == ',')
                $data['service_list'] = substr($data['service_list'], 0, strlen($data['service_list']) - 1);
            
            $data['car_list'] = $this->Dealer_model->get_service_by_dealer_id($dealer_id);
            */

            $this->load->view('header', $data);
            $this->load->view('templates/service/service_detail', $data);

            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer', $data); 
        }
        else
            redirect(base_url().'index.php/service/search');
    }
    
    public function register() {
         $data['message'] = "";
        
        $this->db->where('parent_id', 0);
        $data['service'] = $this->db->get('psacar_service');
        $data['make'] = $this->db->get('psacar_make');
        $data['model'] = $this->db->get('psacar_model');
        $data['location'] = $this->db->get('psacar_location');
        
        $data['template_page'] = 'templates/service/service_register';

        $this->load->view('header', $data);
        $this->load->view('service', $data);
        $this->load->view('footer', $data);       
    }
    
    public function submit() {        
        if (!$this->session->userdata('validated')) {
            $data["message_title"] = "Authentication failure";
            $data["message_body"] = "Please login to list your product.";            
        }
        else if (($this->session->userdata('membership') != 1) && ($this->session->userdata('membership') != 3)) {
            $data["message_title"] = "Access denied";
            $data["message_body"] = "With the current role, you are not allowed to list any car.";
        }
        else {
            $member = $this->input->post('member');
            $location = $this->input->post('location');
            
            $expiration_date = new DateTime(date('Y-m-d'));
            $expiration_date->add(new DateInterval('P30D'));            

            $service = 0;
            if(is_array($this->input->post('service'))){
                foreach ($this->input->post('service') as $a_service) 
                    $service = $a_service;
            }
            
            if(is_array($this->input->post('make'))){
                foreach ($this->input->post('make') as $a_make) {
                    $member_service =  array(
                        'member_id' => $member,
                        'service_id' => $service,
                        'location_id' => $location,
                        'make_id' => $a_make,
                        'status' => 0,
                        'expiration_date' => $expiration_date->format("Y-m-d"));

                    $this->load->database();                            
                    $this->db->insert('psacar_member_service', $this->security->xss_clean($member_service));
                    $member_service_id = $this->db->insert_id();

                    //Reference number
                    $ref_code = '';
                    $service_code_list = $this->db->get_where('psacar_service', array('id' => $service));
                    if ($service_code_list->num_rows != 0) {
                        $service_code = $service_code_list->row();
                        $ref_code = $service_code->code;
                    }

                    $ref_code .= 
                        substr('00000' . $member_service_id, -5) . 
                        random_string('alnum', 3);
                    $data['ref_code'] = strtoupper($ref_code);

                    $this->db->where('id', $member_service_id);
                    $this->db->update('psacar_member_service', $data);                    
                }
            }                        

            $data["message_title"] = "Service registered";
            $data["message_body"] = 
                "Thank you for listing with <a href='" . base_url() . "'>www.psacar.com.</a> Your listing will appear online once it has been approved by our admin team.";
        }
        
        $data['message'] = "";

        $this->load->view('header', $data);
        $this->load->view('information', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);        
    }
    
    public function subcategory(){        
        $service_id = $this->uri->segment(3);
        echo "<option value=''>Please select</option>";
        if ($service_id)
        {
            $service_list = $this->db->get_where('psacar_service', array('parent_id' => $service_id));
            foreach ($service_list->result() as $service)
            {
                echo "<option value='" . $service->id . "' title='" . $service->description . "'>" . $service->name . "</option>";
            }
        }
    }    
        
    public function listed() {
        $data['message'] = "";

        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;
        
        $member_id = $this->uri->segment(3);
        
        $this->load->model('Service_model');
        $service_list = $this->Service_model->get_approved_service_by_member_id($member_id);
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/service/result',
            $service_list->num_rows,
            $limit);

        $service_list = array_slice($service_list->result(), $offset, $limit);
        $data['member_list'] = $service_list;
            
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);         
    } 
   
    public function pending() {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;        

        $member_id = $this->uri->segment(3);
        
        $this->load->model('Service_model');
        $service_list = $this->Service_model->get_pending_service_by_member_id($member_id);
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/service/result',
            $service_list->num_rows,
            $limit);

        $service_list = array_slice($service_list->result(), $offset, $limit);
        $data['member_list'] = $service_list;
            
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);                 
    }

    public function expired() {
        $data['message'] = "";
        
        $offset = $this->uri->segment(3);
        if(strlen($offset) == 0)
            $offset = 0;
        
        $limit = $this->input->post('limit');
        if(strlen($limit) == 0)
            $limit = 10;        

        $member_id = $this->uri->segment(3);
        
        $this->load->model('Service_model');
        $service_list = $this->Service_model->get_expired_service_by_member_id($member_id);        
        
        //Pagination        
        $data['pagination'] = $this->generate_pagination(
            'index.php/service/result',
            $service_list->num_rows,
            $limit);

        $service_list = array_slice($service_list->result(), $offset, $limit);
        $data['member_list'] = $service_list;
            
        $this->load->view('header', $data);
        $this->load->view('templates/member/member_search_result', $data);
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer',$data);        
    }
    
   public function register_search_criteria(
        $make = '', 
        $model = '', 
        $service = '',
        $location = '',
        $year = '') {       
        $search_critieria = array(
            array(
                'field' => 'make',
                'value' => $make),
            array(
                'field' => 'model',
                'value' => $model),
            array(
                'field' => 'service',
                'value' => $service),
            array(
                'field' => 'location',
                'value' => $location),                      
            array(
                'field' => 'year',
                'value' => $year),
        );

        return $search_critieria;
    }
    
    public function generate_pagination($base_url = '', $total_row = 0, $per_page = 0) {       
        $this->load->library('pagination');
        $config['base_url'] = base_url() . $base_url;
        $config['total_rows'] = $total_row;
        $config['per_page'] = $per_page;

        $limiter = '<select name="limit">';
        for($counter = 10; $counter <= 100; ) {
            $limiter .= 
                '<option value="' . $counter . '" ' . 
                ($per_page == $counter ? 'selected' : '') . 
                '>' . $counter . ' per page</option>';
            $counter += 10;
        }
        $limiter .= "</select> | ";
                
        $config['full_tag_open'] = '<div class="pagination">' . $limiter;
        $config['full_tag_close'] = '</div>';
        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }
}